package com.cjh.wechat.core.controller;

import java.util.Iterator;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.cjh.wechat.commom.pojo.NotifyGroup;
import com.cjh.wechat.commom.pojo.User;
import com.cjh.wechat.commom.type.ApplyStatus;
import com.cjh.wechat.commom.vo.PageVo;
import com.cjh.wechat.core.service.NotifyGroupService;
import com.cjh.wechat.core.service.UserService;

@Controller
@RequestMapping("/notifygroup")
public class NotifyGroupController
{
    @Resource
    private NotifyGroupService notifyGroupService;
    @Resource
    private UserService        userService;
    private static final int   PAGE_SIZE = 10;
    
    @ResponseBody
    @RequestMapping(value = "/findByGid", method = RequestMethod.POST)
    public PageVo findByGid(String gid, int page)
    {
        List<NotifyGroup> notifyGroups = notifyGroupService.findByGid(gid, page, PAGE_SIZE);
        Iterator<NotifyGroup> iterator = notifyGroups.iterator();
        while (iterator.hasNext())
        {
            NotifyGroup notifyGroup = iterator.next();
            User user = userService.findById(notifyGroup.getFromUid());
            notifyGroup.setUser(user);
        }
        int total = notifyGroupService.findByGidSum(gid);
        return buildPageVo(total, notifyGroups);
    }
    
    @ResponseBody
    @RequestMapping(value = "/agree", method = RequestMethod.POST)
    public String agree(Integer no)
    {
        try
        {
            notifyGroupService.agreeApply(no);
            return "success";
        }
        catch (Exception e)
        {
            return "fail";
        }
    }
    
    @ResponseBody
    @RequestMapping(value = "/refuse", method = RequestMethod.POST)
    public String refuse(Integer no)
    {
        try
        {
            notifyGroupService.refuseApply(no);
            return "success";
        }
        catch (Exception e)
        {
            return "fail";
        }
    }
    
    @ResponseBody
    @RequestMapping(value = "/updateByGid", method = RequestMethod.POST)
    public String updateByGid(@ModelAttribute NotifyGroup notifyGroup)
    {
        notifyGroupService.updateByGidSelective(notifyGroup);
        return "success";
    }
    
    @ResponseBody
    @RequestMapping(value = "/apply", method = RequestMethod.POST)
    public String apply(@ModelAttribute NotifyGroup notifyGroup)
    {
        if (notifyGroupService.isExist(notifyGroup.getGid(), notifyGroup.getFromUid(), ApplyStatus.INIT.value))
        {
            return "repeat";
        }
        else
        {
            try
            {
                notifyGroupService.apply(notifyGroup);
                return "success";
            }
            catch (Exception e)
            {
                return "fail";
            }
        }
    }
    
    private PageVo buildPageVo(int total, List<NotifyGroup> notifyGroups)
    {
        PageVo pageVo = new PageVo();
        if (total == 0)
        {
            pageVo.setPages(1);
        }
        else
        {
            pageVo.setPages(total % PAGE_SIZE == 0 ? total / PAGE_SIZE : total / PAGE_SIZE + 1);
        }
        pageVo.setData(notifyGroups);
        return pageVo;
    }
}
